package com.LFLWZ.ServiceImpl;


import com.LFLWZ.beans.Dto.adminListQuery;
import com.LFLWZ.beans.POJO.*;
import com.LFLWZ.beans.Vo.ClassroomCourseVo;
import com.LFLWZ.mapper.*;
import com.LFLWZ.beans.Dto.Page;
import com.LFLWZ.utils.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class courseImpl {

    @Autowired
    private ClassroomCourseMapper classroomCourseMapper;
    @Autowired
    private ColumnMapper columnMapper;
    @Autowired
    private CourseColumnMapper courseColumnMapper;
    @Autowired
    private LecturerMapper lecturerMapper;
    @Autowired
    private ClassroomCourseDetailsMapper classroomCourseDetailsMapper;

    public Page<ClassroomCourse> listcourse(adminListQuery adminListQuery){
        adminListQuery.setOffset((adminListQuery.getPage()-1)*adminListQuery.getLimit());
        Page page=new Page<>();
        page.setTotal(classroomCourseMapper.count());
        page.setRows(classroomCourseMapper.queryAllByLimit(adminListQuery));
        return page;
    }

    public List<CourseType> types(){
        return classroomCourseMapper.selCourseType(new CourseType());
    }

    public List<filtrate> finFiltrates(){
        return classroomCourseMapper.sel_filtrate();
    }

    public List<Column> columns(){
        return columnMapper.sel(new Column());
    }
    public List<Lecturer> lecturer(){
        return lecturerMapper.queryAll();
    }

    public ClassroomCourseVo insert(ClassroomCourseVo classroomCourse) {
        this.classroomCourseMapper.insert(classroomCourse);
            /**
             * 赋关键值
             */
            CourseFiltrate cf =new CourseFiltrate(classroomCourse.getId(),classroomCourse.getCid());
            /**
             * 插入
             */
            classroomCourseMapper.inFiltrate(cf);

            CourseColumn courseColumn=new CourseColumn(classroomCourse.getId(),classroomCourse.getSid());
            courseColumnMapper.insert(courseColumn);
        if (EmptyUtils.isNotEmpty(classroomCourse.getClassroomCourseDetails())){
            int i=1;
            for (ClassroomCourseDetails classroomCourseDetails:classroomCourse.getClassroomCourseDetails()){
                if (classroomCourseDetails.getTitle()==null||classroomCourseDetails.getTitle().equals("")){
                    classroomCourseDetails.setTitle(classroomCourse.getCoursebrief());
                }
                if (classroomCourseDetails.getCourseid()==null){
                    classroomCourseDetails.setCourseid(classroomCourse.getId());
                }
                if (classroomCourseDetails.getNumber()==null){
                    classroomCourseDetails.setNumber(i);
                }
                classroomCourseDetailsMapper.insert(classroomCourseDetails);
                i++;
            }
        }
        return classroomCourse;
    }

}
